//
//  CollectVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/11/3.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "CollectVC.h"
#import "IGAddPhotoCell.h"
#import "IGGetCollectWorksApi.h"
#import "IGAddAtlasDetailApi.h"
#import "CollecteWorkModel.h"

@interface CollectVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (strong, nonatomic) IBOutlet UITableView *collectTable;
@property(nonatomic,strong)IGGetCollectWorksApi *collectWorkApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *pageNumber;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, copy) NSString *queryTime;
@property(nonatomic,strong)IGAddAtlasDetailApi *addAltasDetailApi;
@end

@implementation CollectVC
#pragma mark -懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectTable.backgroundColor =  [UIColor c2Color];
    [self.collectTable registerClass:[IGAddPhotoCell class] forCellReuseIdentifier:@"addPhotoCell"];
    self.collectTable.delegate=self;
    self.collectTable.dataSource=self;
    self.collectTable.emptyDataSetSource = self;
    self.collectTable.emptyDataSetDelegate = self;
    _pageSize = @"10";
    _pageNumber = @"0";
    _queryTime = @"";
    self.collectWorkApi=[[IGGetCollectWorksApi alloc]initWithAid:self.aid PageNumber:_pageNumber pageSize:_pageSize queryTime:_queryTime];
    self.addAltasDetailApi=[IGAddAtlasDetailApi new];
    
    @KWeakObj(self)
    [self.collectTable ig_setHeaderWithRefreshingBlock:^{
        _pageNumber = @"0";
        _pageSize = @"10";
        _queryTime = @"";
        [selfWeak requestDate];
    }];
    [self requestDate];
}
-(void)requestDate{
    self.collectWorkApi.pageNumber=_pageNumber;
    self.collectWorkApi.pageSize=_pageSize;
    self.collectWorkApi.queryTime=_queryTime;
    @KWeakObj(self)
    [self.collectWorkApi startWithDecodeClass:[IGGetCollectWorksResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.collectTable ig_endHeaderRefreshing];
        [self.collectTable ig_endFooterRefreshing];
        if (responseObject) {
            IGGetCollectWorksResp *resp=responseObject;
            IGGetCollectWorksModel*model=resp.data;
            if (model.lstWork&&model.lstWork.count>0) {
                [selfWeak setupFooter];
            }
            else
            {
                [self.collectTable ig_endFooterRefreshingWithNoMoreData];
    
            }
            _queryTime = model.queryTime;
            if ([_pageNumber isEqualToString:@"0"]){
                _dataArr = [NSMutableArray arrayWithArray:model.lstWork];
                }
            else
            {
                [_dataArr addObjectsFromArray:model.lstWork];
            }
          [self.collectTable reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.collectTable ig_setFooterWithRefreshingBlock:^{
        _pageNumber = [NSString stringWithFormat:@"%ld", [_pageNumber integerValue] + 1];
        [selfWeak requestDate];
    }];
}

- (void)scrollToTOP
{
    [self.collectTable setContentOffset:CGPointMake(0, 0) animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.collectTable ig_beginHeaderRefreshing];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count > 0 ? self.dataArr.count : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addPhotoCell"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (cell==nil)
    {
        cell = [[IGAddPhotoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"addPhotoCell"];
    }
    cell.data=_dataArr[indexPath.section];
    CollecteWorkModel *workInfo=_dataArr[indexPath.section];
    cell.collectInfo=workInfo;
    __weak typeof(self) weakSelf = self;
    cell.attenBlock = ^{
        weakSelf.addAltasDetailApi.aId = self.aid;
        weakSelf.addAltasDetailApi.pids= workInfo.pId;
        [weakSelf.addAltasDetailApi startWithDecodeClass:[IGAddAtlasDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject) {                
                workInfo.ifExist = [workInfo.ifExist boolValue]?@"0":@"1";
                
                [weakSelf.collectTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//                 [weakSelf.collectTable ig_beginHeaderRefreshing];
            }
        }];
    };
    [cell setNeedsLayout];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 10;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"您还没有收藏任何作品~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No Collection", nil) attributes:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
